package org.csu.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.demo.common.CommonResponse;
import org.csu.demo.entity.Category;
import org.csu.demo.entity.Item;
import org.csu.demo.entity.ItemInventory;
import org.csu.demo.entity.Product;
import org.csu.demo.persistence.CategoryMapper;
import org.csu.demo.persistence.ItemInventoryMapper;
import org.csu.demo.persistence.ItemMapper;
import org.csu.demo.persistence.ProductMapper;
import org.csu.demo.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

@Service
public class CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemInventoryMapper itemInventoryMapper;

    public CommonResponse<List<Category>> getCategoryList(){
        List<Category> categoryList = categoryMapper.selectList(null);
        if (categoryList.isEmpty()){
            return CommonResponse.creatForSuccessMessage("没有分类信息");
        }
        return CommonResponse.creatForSuccess(categoryList);
    }

    public CommonResponse<Category> getCategory(String categoryId){
        Category category = categoryMapper.selectById(categoryId);
        if (category == null){
            return CommonResponse.creatForSuccessMessage("没有该id的category");
        }
        return CommonResponse.creatForSuccess(category);
    }

    public CommonResponse<List<Product>> getProductList(String categoryId){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper);
        if (productList.isEmpty()){
            return CommonResponse.creatForSuccessMessage("该id的category下没有product");
        }
        return CommonResponse.creatForSuccess(productList);
    }

    public CommonResponse<Product> getProduct(@PathVariable("id") String productId){
        Product product = productMapper.selectById(productId);
        if (product == null){
            return CommonResponse.creatForSuccessMessage("没有该id的product");
        }
        return CommonResponse.creatForSuccess(product);
    }

    public CommonResponse<List<ItemVO>> getItemList(@PathVariable("id") String productId){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if (itemList.isEmpty()){
            return CommonResponse.creatForSuccessMessage("该id的product下没有item");
        }
        Product product = productMapper.selectById(productId);
        List<ItemVO> itemVOList = new ArrayList<>();
        for (Item item : itemList){
            ItemVO itemVO = itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.creatForSuccess(itemVOList);
    }

    private ItemVO itemToItemVO(Item item,Product product){
        ItemVO itemVO = new ItemVO();
        itemVO.setItemid(item.getItemId());
        itemVO.setProductid(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitcost(item.getUnitcost());
        itemVO.setSupplier(item.getSupplier());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());

        itemVO.setCategory(product.getCategory());
        itemVO.setName(product.getName());
        itemVO.setDescn(product.getDescn());

        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
        itemVO.setQuantity(itemInventory.getQuantity());

        return itemVO;
    }
}
